// pages/index/listQuery/listQuery.js
const util = require("../../../../utils/util");
const app = getApp();
import WxValidate from '../../../../utils/WxValidate';
let modelId;
let citycode;
let cityName;
let toggleCollection = true;
Page({
    data: {
        attrList: [],
        listName: "",
        isNaviBack: false,
        modelId: '',
        scroll: false,
        cityName: '深圳',
        subscribeText: '订阅',
        modelId: 1
    },
    onLoad: function (options) {
        toggleCollection = true;
        cityName = options.cityName ? options.cityName : ks.getStorageSync("city");
        citycode = options.city ? decodeURIComponent(options.city) : ks.getStorageSync("citycode");
        modelId = options.modelId;
        this.setData({
            cityName,
            cityCode: citycode,
            modelId
        })
        if (modelId == 1) {
            ks.redirectTo({
                url: `/pages/tools/yaohaojieguo/yaohaojieguo?city=${this.data.cityName}`,
            })
            return;
        }
        util.getUrl(app.globalData.url).then(res => {
            if (modelId) {
                util.bstrequest('listQuery.php', {
                    ac: 'model',
                    model_id: modelId
                }, 'POST').then(res => {
                    this.setData({
                        attrList: res.data.data,
                        listName: res.data.data[0].name,
                        modelId
                    });
                    this.initValidate();
                });
                if (modelId == 1) {
                    util.checkSubscribe("车牌摇号结果", this.data.cityName).then(result => this.setData({
                        subscribeText: result ? '已订阅' : '订阅'
                    }))
                }
            } else {
                let $getInitializationInfo = {
                    ac: 'type',
                    citycode,
                };
                util.bstrequest('listQuery.php', $getInitializationInfo, 'POST').then(res => {
                    if (res.data) {
                        let initInfo = JSON.parse(JSON.stringify(res.data));
                        if (initInfo.type)
                            modelId = initInfo.type[0].model_id;
                        this.setData({
                            attrList: initInfo.attr,
                            listName: initInfo.type[0].name,
                            modelId
                        });
                    }
                    // this.initValidate();
                });

            }
        });
    },
    onShow() {
        // 显示选择的模型查询属性
        if (this.data.isNaviBack) {
            let $getAttrList = {
                ac: "model",
                model_id: this.data.modelId
            };
            util.bstrequest('listQuery.php', $getAttrList, 'POST').then(res => {
                this.setData({
                    attrList: res.data.data
                });
                // this.initValidate();
            });
        }
    },
    // 订阅成功
    subsuccess() {
        this.setData({
            subscribeText: "已订阅"
        })
        ks.showToast({
            title: '订阅成功，待结果公布后会有提醒',
            icon: 'none'
        })
    },
    choiceType(e) {
        ks.redirectTo({
            url: `../list/list?modelId=${modelId}&citycode=${citycode}&cityName=${this.data.cityName}`,
        });
    },

    showMoadal(error) {
        ks.showModal({
            title: '提示',
            content: error.msg,
            showCancle: false,
        })
    },
    initValidate() {
        const rule = {};
        const message = {};
        // 循环属性，添加验证规则
        if (this.data.attrList) {
            this.data.attrList.forEach(v => {
                rule[v.attr_id] = {
                    required: true,
                };
                message[v.attr_id] = {
                    required: `请输入${v.attr_name}`,
                };
            });
        }
        //  创建实例对象
        this.WxValidate = new WxValidate(rule, message);
    },
    search(e) {
        if (this.WxValidate)
            if (!this.WxValidate.checkForm(e.detail.value)) {
                const error = this.WxValidate.errorList[0];
                this.showMoadal(error);
                return false;
            } else
                ks.navigateTo({
                    url: `../result/result?attr=${JSON.stringify(e.detail.value)}&modelId=${this.data.modelId}&cityName=${cityName}`,
                });
        else {
            this.initValidate();
            this.search(e);
        }

    },
    onShareAppMessage: function () {
        return {
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
            path: `pages/tools/listQuery/index/index?citycode=${this.citycode}&modelId=${this.data.modelId}&cityName=${this.data.cityName}`,
            title: this.data.cityName + '车牌摇号结果查询'
        }
    },
    onPageScroll() {
        if (toggleCollection) {
            this.setData({
                scroll: true,
            });
            toggleCollection = false;
        }

    },
    closeCollection() {
        toggleCollection = false;
    },
    openCollection() {
        toggleCollection = true;
    }
})